var fruitObj = function() {
	this.alive = [];
	this.long = [];
	this.x = [];
	this.y = [];
	this.sheep = new Image();
	this.cat = new Image();
	this.dog = new Image();
	this.tiger = new Image();
	this.sheep_cry = new Image();
	this.cat_cry = new Image();
	this.dog_cry = new Image();
	this.tiger_cry = new Image();
}; 
fruitObj.prototype.num = 4;
fruitObj.prototype.init =function(){
	for (var i = 0; i < this.num; i++) {
		this.alive[i] = false;
		this.x[i] = 0;
		this.y[i] = 0;
		//this.born(i);
	}
	this.sheep.src = "./src/d11.png";
	this.cat.src = "./src/d22.png";
	this.dog.src = "./src/d33.png";
	this.tiger.src = "./src/d44.png";
	this.sheep_cry.src = "./src/d11_cry.png";
	this.cat_cry.src = "./src/d22_cry.png";
	this.dog_cry.src = "./src/d33_cry.png";
	this.tiger_cry.src = "./src/d44_cry.png";
}

var totleWidth = 340.0;
var picNum = 4.0;
var ptpLong = totleWidth/picNum;
fruitObj.prototype.draw =function(){
	for (var i = 0; i < this.num; i++) {
		// console.log(i);
		if(!this.long[i]){
			this.long[i]=0;
		}
		if (this.long[i]+this.x[i] >= totleWidth) {
			this.alive[i] = false;
		}
		this.long[i] += 0.05 * deltaTime;
		var totle = this.long[i]+this.x[i];
		// ctx2.drawImage(this.sheep,totle - this.sheep.width*0.5,this.y[i] - this.sheep.height*0.5);
		
		if ( i == 0) {
			ctx2.drawImage(this.sheep,totle - this.sheep.width*0.5,this.y[i] - this.sheep.height*0.5);
		}
		if ( i == 1) {
			ctx2.drawImage(this.cat,totle - this.cat.width*0.5,this.y[i] - this.cat.height*0.5);
		}
		if ( i == 2) {
			ctx2.drawImage(this.dog,totle - this.dog.width*0.5,this.y[i] - this.dog.height*0.5);		
		}
		if ( i == 3) {
			ctx2.drawImage(this.tiger,totle - this.tiger.width*0.5,this.y[i] - this.tiger.height*0.5);		
		}
	}    
}

fruitObj.prototype.born =function(i){
	var arr = [];
	for(var j=0; j<totleWidth; j=j+ptpLong) { 
	    arr.push(j);
	}
	if(this.long[i]+this.x[i] >= totleWidth){
		this.long[i]=0;
		this.x[i]= 0;
	}else{
		this.x[i] = -arr[i];
	}
	
	this.y[i] = 333;
	this.alive[i] = true;
}
var flag = true;
fruitObj.prototype.up =function(i,st){
	this.y[i] = mom.y+35;
	this.x[i] = mom.x - this.long[i];
	if ( i == 0) {
		this.sheep = this.sheep_cry;
	}
	if ( i == 1) {
		this.cat = this.cat_cry;
	}
	if ( i == 2) {
		this.dog = this.dog_cry;
	}
	if ( i == 3) {
		this.tiger = this.tiger_cry;
	}
	if(mom.y<= 146){
		// console.log(this.sheep);
		this.sheep_cry.src = "./src/d11.png";
		this.cat_cry.src = "./src/d22.png";
		this.dog_cry.src = "./src/d33.png";
		this.tiger_cry.src = "./src/d44.png";
		// window.location.href="aiyaya_result.html"; 
		this.dead(i);
		if ( i == 0) {
			this.sheep = this.sheep_cry;
		}
		if ( i == 1) {
			this.cat = this.cat_cry;
		}
		if ( i == 2) {
			this.dog = this.dog_cry;
		}
		if ( i == 3) {
			this.tiger = this.tiger_cry;
		}
		$(".cover").hide();
		$(".result").show();
		$(".zhong").show();
		window.cancelAnimationFrame(st);
	}
}

fruitObj.prototype.dead =function(i){
	this.alive[i] = false;
}

function fruitMonitor(){
	var num = 0;
	for(var i = 0;i < fruit.num;i++){
		if (fruit.alive[i]) {num++}
	}
	if (num < picNum) {
		sendFruit();
		return;
	}
}

function sendFruit(){
	for(var i = 0;i < fruit.num;i++){
		if (!fruit.alive[i]) {
			fruit.born(i);
			return;
		}
	}
}
